import java.util.concurrent.DelayQueue;

public class runDelay {
    final static DelayQueue<Bean> delayQueue = new DelayQueue<Bean>();
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<20;i++){
                    int delay = (int)(Math.random()*5000);
                    Bean bean = new Bean(delay,i);
                    delayQueue.put(bean);
                    System.out.println("入队："+bean.toString());
                }
            }
        }).start();
        while(delayQueue.size()<20) {
            //等待延迟队列装满
            System.out.println("现在的队列元素是:"+delayQueue.size()+"个");
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(!delayQueue.isEmpty()){
                    try{
                        Bean bean=(Bean) delayQueue.take();
                        System.out.println("出队："+bean.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
}
